from lxml import etree
doc='''
    <div>
        <ul>
             <li class="item-0"><a href="link1.html">first item</a></li>
             <li class="item-1"><a href="link2.html">second item</a></li>
             <li class="item-inactive"><a href="link3.html">third item</a></li>
             <li class="item-1"><a href="link4.html">fourth item</a></li>
             <li class="item-0"><a href="link5.html">fifth item</a> # 注意，此处缺少一个 </li> 闭合标签
         </ul>
     </div>
    '''
html=etree.HTML(doc)
# div=html.xpath('/html/body/div')
div=html.xpath('//li')
print(div)
print(type(div))
print(len(div))
print(div[0])
print(type(div[0]))






# print(html.xpath('/html/body/div'))
# print(type(html.xpath('/html/body/div')))
# print(len(html.xpath('/html/body/div')))




# print(html.xpath("//li"))
# print(html.xpath('//li[last()]'))
# print(html.xpath('//li[@class="item-0"]'))
# print(html.xpath("//li[@class='item-inactive']/a/text()"))

# print(html.xpath("//li/class[contains(text(),'item-1')]"))




# #最后一个li被限定了
# print(html.xpath("//li[last()]/a/text()"))
# print(html.xpath("//li/a[last()]/text()"))
# print(html.xpath("//li/a[contains(text(),'third')]"))





# html=etree.HTML(doc)
# print(str(etree.tostring(html), 'utf-8'))
# print(type(html))
# div1=html.xpath('/html/body/div')
# div2=html.xpath('//div')
# print(type(div2))
# print(len(div2))
# print(type(div2[0]))
# ul1=html.xpath('//ul')
# print(type(ul1))
# print(len(ul1))
# print(type(ul1[0]))

# 把文本转换成一个文档树对象
# html = etree.HTML(doc)
# result = etree.tostring(html)
# print(str(result, 'utf-8'))





# html = etree.parse('index.html')
# result = etree.tostring(html, pretty_print=True)    #pretty_print=True 会格式化输出
# print(result)






# html = etree.HTML(doc)
# div=html.xpath('//div/text()')



